﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace psafth.RCTiming.Main.Pages
{
    /// <summary>
    /// Interaction logic for SettingsGeneral.xaml
    /// </summary>
    public partial class SettingsGeneral : Page
    {
        public SettingsGeneral()
        {
            InitializeComponent();
            //tbIpAddress.Text = Properties.Settings.Default.DecoderIPAddress;
            //tbPort.Text = Properties.Settings.Default.DecoderTcpPort.ToString();
            //cbSyncTime.IsChecked = Properties.Settings.Default.DecoderUseClockSync;

            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.UserLogo))
            {
                SetLogo();
            }

            tbHornCOMPort.Text = Properties.Settings.Default.SerialPort;
            tbMinimumLapTime.Text = Properties.Settings.Default.MinimumLapTime.ToString();
        }

        private void SetLogo()
        {
            byte[] logoBytes = Convert.FromBase64String(Properties.Settings.Default.UserLogo);

            MemoryStream ms = new MemoryStream(logoBytes);

            Bitmap bitmap = new Bitmap(ms);

            imgLogo.Source = ConvertBitmapToBitmapImage(bitmap);
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            Views.AMBStatus t = new Views.AMBStatus();

            t.Show();
        }

        //private void tbIpAddress_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    Properties.Settings.Default.DecoderIPAddress = tbIpAddress.Text;
        //    Properties.Settings.Default.Save();
        //}

        //private void tbPort_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    Properties.Settings.Default.DecoderTcpPort = Convert.ToInt32(tbPort.Text);
        //    Properties.Settings.Default.Save();
        //}

        //private void cbSyncTime_Checked(object sender, RoutedEventArgs e)
        //{
        //    Properties.Settings.Default.DecoderUseClockSync = (cbSyncTime.IsChecked.HasValue) ? cbSyncTime.IsChecked.Value : false;
        //    Properties.Settings.Default.Save();
        //}

        private void btnSetLogo_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                byte[] fileBytes = File.ReadAllBytes(openFileDialog.FileName);

                string bmp = Convert.ToBase64String(fileBytes);

                Properties.Settings.Default.UserLogo = bmp;
                Properties.Settings.Default.Save();

                SetLogo();
            }
        }

        private BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
        {
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Png);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
            bitmapImage.EndInit();

            return bitmapImage;
        }

        private void btnTestHorn_Click(object sender, RoutedEventArgs e)
        {
            SoundPlayer.PlaySound(0, 1000, true);
        }

        private void tbMinimumLapTime_TextChanged(object sender, TextChangedEventArgs e)
        {
            Properties.Settings.Default.MinimumLapTime = Int32.Parse(tbMinimumLapTime.Text);
            Properties.Settings.Default.Save();
        }

        private void tbHornCOMPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            Properties.Settings.Default.SerialPort = tbHornCOMPort.Text;
            Properties.Settings.Default.Save();
        }

        private void btnTestSound_Click(object sender, RoutedEventArgs e)
        {
            SoundPlayer.PlaySound(3, 300, true);
            SoundPlayer.PlaySound(2, 300, false);
            SoundPlayer.PlaySound(1, 300, true);
            SoundPlayer.PlaySound(0, 300, false);
        }
    }
}
